import { _decorator, Component, find, Node, random } from 'cc';
import { Ins } from '../Ins';
import { DataMgr } from './DataMgr';
import { PoolMgr } from './PoolMgr';
import { Game } from '../game/Game';
import { ENUM_AUDIO_CLIP, ENUM_GAME_STATUS, ENUM_UI_LAYER } from '../Enum';
import { AudioMgr } from './AudioMgr';
import { getRand } from '../Util';
import { SdkMgr } from './SdkMgr';
import { ReqMgr } from './ReqMgr';
const { ccclass, property } = _decorator;

@ccclass('GameMgr')
export class GameMgr extends Component {
    stage: Node = null
    level_num: number = -1

    onLoad() {
        Ins.initGameMgr(this)
        this.stage = find('stage', this.node)
    }

    onGameStart() {
        DataMgr.ins.reset()
        this.init()
    }

    onGameCheck() {
        if (DataMgr.ins.gameStatus == ENUM_GAME_STATUS.UNACTIVE) return
        if (DataMgr.ins.cleared >= DataMgr.ins.total) {
            this.onGameOver(ENUM_UI_LAYER.WIN)
        }
    }

    async onGameOver(layer: ENUM_UI_LAYER) {
        Ins.UIMgr.setMainCountDown(false)
        DataMgr.ins.gameStatus = ENUM_GAME_STATUS.UNACTIVE
        if (Ins.UIMgr.isActive(ENUM_UI_LAYER.EXIT)) Ins.UIMgr.toggle(ENUM_UI_LAYER.EXIT, false)
        if (Ins.UIMgr.isActive(ENUM_UI_LAYER.ANSWER)) Ins.UIMgr.toggle(ENUM_UI_LAYER.ANSWER, false)
        if (layer == ENUM_UI_LAYER.LOSE) {
            AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.LOSE)
            Ins.UIMgr.toggle(ENUM_UI_LAYER.LOSE)
        } else {
            if (!DataMgr.ins.isHard) {
                await Ins.FadeMgr.fadeIn()
                AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.WOO)
                DataMgr.ins.isHard = true
                this.onGameStart()
            } else {
                AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.WIN)
                this.level_num = -1
                DataMgr.ins.isHard = false
                // 排行榜
                if (SdkMgr.ins.getPlatform() && DataMgr.ins.user) {
                    if (DataMgr.ins.user.openid) {
                        // console.log('提交数据', { ...DataMgr.ins.user, score: 5 })
                        ReqMgr.ins.request('rank-set', 'post', {
                            openid: DataMgr.ins.user.openid,
                            nickname: DataMgr.ins.user.nickname,
                            avatar: DataMgr.ins.user.avatar,
                            gender: DataMgr.ins.user.gender,
                            score: 5
                        })
                    }
                }
                let level = DataMgr.ins.currentLevel
                level += 1
                level = Math.min(level, DataMgr.ins.hardLevel + 1)
                DataMgr.ins.currentLevel = level
                DataMgr.ins.save()
                Ins.UIMgr.toggle(ENUM_UI_LAYER.WIN)
            }
        }
    }

    async onGameSkill(i: number) {
        if (i == 0) {
            await Ins.FadeMgr.fadeIn()
            DataMgr.ins.isResetSkillBtn = false
            this.onGameStart()
        } else if (i == 1) {
            Ins.UIMgr.toggleMainSkillTip(true)
        } else if (i == 2) {
            Ins.UIMgr.toggle(ENUM_UI_LAYER.ANSWER)
        }
    }

    init() {
        this.stage.removeAllChildren()
        const level_prefix = DataMgr.ins.isHard ? 'LH' : 'L'
        if (this.level_num < 0) {
            this.level_num = DataMgr.ins.currentLevel
            if (DataMgr.ins.currentLevel > DataMgr.ins.hardLevel) {
                this.level_num = getRand(1, DataMgr.ins.hardLevel)
            }
        }
        DataMgr.ins.level = this.level_num
        const level = PoolMgr.ins.getNode(`${level_prefix}${DataMgr.ins.level}`, this.stage)
        // const level = PoolMgr.ins.getNode(`LH1`, this.stage)
        const levelComp = level.getComponent(Game)
        levelComp.init()
        Ins.UIMgr.resetMainLayer()
        DataMgr.ins.gameStatus = ENUM_GAME_STATUS.ACTIVE
        Ins.FadeMgr.fadeOut()
        Ins.UIMgr.setMainCountDown()
    }
}


